package com.zhiyume.controller;

import com.zhiyume.dao.entity.TExtension;
import com.zhiyume.dao.entity.TSplash;
import com.zhiyume.api.util.SplashEnum;
import com.zhiyume.service.ExtentsionService;
import com.zhiyume.service.SplashService;
import com.zhiyume.service.util.StringUtil;
import com.zhiyume.vo.SplashVO;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wang on 17/11/15.
 */
@Controller
@RequestMapping("/splash")
public class SplashController {
    @Autowired
    private SplashService splashService;

    @Autowired
    private ExtentsionService extentsionService;

    @Value("${custom.domain.domain}")
    String url;

    private static final String URL = "page/splash";

    private static final Log log = LogFactory.getLog(SplashController.class.getName());


    @RequestMapping("/addPage")
    public ModelAndView addPage(Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(URL+"/addSplash");
        TSplash search = new TSplash();
        //查询结果
        TSplash tSplash = new TSplash();

        TExtension tExtension = new TExtension();
        try {
            tSplash = splashService.getLastSplash(search);
            TExtension extension = new TExtension();
            extension.setId(tSplash.getFromId());
            tExtension = extentsionService.getExtentsion(extension);

        }catch (Exception e){
            log.error(e.getMessage());
        }
        model.addAttribute("splash",tSplash);
        model.addAttribute("extension",tExtension);
        model.addAttribute("type", SplashEnum.getAll());
        return modelAndView;
    }


    /**
     * 新增开屏信息
     * @param splashVO
     * @return
     */
    @RequestMapping("/addSplash")
    @ResponseBody
    public Map addSplash(@ModelAttribute("form") SplashVO splashVO){
        String result = "success";
        Map map = new HashMap();
        TSplash tSplash = new TSplash();
        TExtension tExtension = new TExtension();

        BeanUtils.copyProperties(splashVO, tSplash);

        BeanUtils.copyProperties(splashVO, tExtension);
        if("0".equals(splashVO.getType()))
            if(StringUtil.isBlank(splashVO.getScheme()))
            tExtension.setScheme(url);
        try {
            Long from_id = extentsionService.addExtension(tExtension);
            tSplash.setFromId(from_id);
            splashService.addSplash(tSplash);

        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        map.put("result",result);
        return map;
    }

}
